﻿<!DOCTYPE html>

<html lang="zh-cn">
<head>
    <meta name="viewport" content="width=device-width" />
    <title>案件信息要素中心</title>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link href="../lib/layui/css/layui.min.css" rel="stylesheet" />
    <link href="../x-admin/css/font.css" rel="stylesheet" />
    <link href="../x-admin/css/xadmin.css" rel="stylesheet" />
    <script type="text/javascript" src="../lib/Base64/base64.min.js"></script>
    <script type="text/javascript" src="../linq.js/linq.js"></script>
    <script src="../lib/cookie/cookie.js"></script>
    <style>
        .unoperate {
            display: none;
        }
    </style>
</head>
<body style="background-color:#eeeeee">
    <div class="layui-card" style="margin-left:0px">
        <div class="layui-card-body ">
            <form class="layui-form layui-col-space5" id="searchForm" lay-filter="searchForm">
                <input class="layui-input" type="hidden" name="dir" id="dir" />
                <div class="layui-input-inline layui-show-xs-block">
                    <input class="layui-input" placeholder="文件名(支持模糊匹配)" name="fname" id="fname">
                </div>
                <div class="layui-input-inline layui-show-xs-block">
                    <a class="layui-btn" href="javascript:;" lay-submit lay-filter="sreach">
                        <i class="layui-icon">&#xe615;</i>
                    </a>
                </div>
            </form>
        </div>
        <div class="layui-card-header" style="">
            <a class="layui-btn operateBtn unoperate" href="javascript:;" id="uploadfile">
                <i class="layui-icon">&#xe62f;</i>上传文件
            </a>
        </div>
        <div class="layui-card-body" style="">
            <table class="layui-table layui-form" id="caseelement" name="caseelement" lay-filter="caseelement">
            </table>
        </div>
    </div>
    <script src="../lib/layui/layui.js"></script>
    <script type="text/html" id="barDemo">
        <a title="查看" href="javascript:;" lay-event="detail">
            <i class="layui-icon">&#xe63c;</i>查看
        </a>
        <a  title="下载" class="operateBtn" href="javascript:;" lay-event="downloadf" >
            <i class="layui-icon">&#xe601;</i>下载
        </a>
    </script>
    <script type="text/javascript">
        function getrequest() {
            var url = location.search; //获取url中"?"符后的字串
            var theRequest = new Object();
            if (url.indexOf("?") != -1) {
                var str = url.substr(1);
                strs = str.split("&");
                for (var i = 0; i < strs.length; i++) {
                    theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
                }
            }
            return theRequest;
        }
        layui.$(function () {
            layui.$("#dir").val(getrequest().dir);
        });
        layui.use(['upload', 'table'], function () {
            var table = layui.table;
            var upload = layui.upload;
            $tab=table.render({
                elem: '#caseelement'
                , method: 'get'
                , skin: 'row'
                , even: true
                , page: true
                , url: localStorage.getItem('hosttingaddr') + '/api/CaseElementCfgFile/index?dir=' + getrequest().dir  //数据接口
                , headers: {
                    user: getCookie("userinfo"),
                    version: getCookie("version")
                }
                , cols: [[ //表头
                    { field: 'name', title: '文件名称'}
                    , { field: 'parentPath', title: '文件路径'}
                    , { field: 'fileSize', title: '文件大小'}
                    , { field: 'createdTime', title: '文件创建时间'}
                    , { field: 'updateTime', title: '文件更新时间'}
                    , { title: "操作", align: 'center', toolbar: '#barDemo' }
                ]]
                , where: getWhere()
                ,done: function (res) {
                    if (res.isOperate != 1) {
                        layui.$(".operateBtn").addClass("unoperate");
                    }
                    else {
                        layui.$(".operateBtn").removeClass("unoperate");
                    }
                }
            });

            function getWhere() {
                var where = layui.form.val("searchForm");
                return where;
            };
            //搜索按钮事件
            layui.form.on("submit(sreach)", function () {
                var where = getWhere();
                $tab.reload({
                    where: where,
                    page: { curr: 1 }
                });
                return false;
            });

            //监听行工具事件
            table.on('tool(caseelement)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
                var data = obj.data //获得当前行数据
                    , layEvent = obj.event; //获得 lay-event 对应的值
                var fileN = data.path;
                var fileName = data.name;
                if (layEvent == 'detail') {
                    layer.open({
                        type: 2,
                        title: '模块配置文件内容',
                        maxmin: true,
                        shadeClose: true, //点击遮罩关闭层
                        area: ['1600px', '800px'],
                        content: 'caseelementdetailinfo.html' + '?fileName=' + fileN// iframe链接地址
                    });
                    
                } else if (layEvent == 'downloadf') {
                    //进行文件下载
                    var xhr = new XMLHttpRequest();
                    xhr.open("get", localStorage.getItem('hosttingaddr') + '/api/CaseElementCfgFile/downloadfile?fileName=' + fileN, true);
                    xhr.responseType = "blob";
                    xhr.setRequestHeader("user", getCookie("userinfo"));
                    xhr.setRequestHeader("version", getCookie("version"));
                    xhr.onload = function (re) {
                        if (this.status == 200) {
                            var blob = this.response;
                            var reader = new FileReader();
                            reader.readAsDataURL(blob);
                            reader.onload = function (e) {
                                var a = document.createElement('a');
                                a.download = fileName;
                                a.href = e.target.result;
                                document.body.appendChild(a);
                                a.click();
                                document.body.removeChild(a);
                            }
                        }
                    };
                    xhr.send();
                }
            });

            //支持上传
            var $ = layui.$;
            //执行实例
            var uploadInst = upload.render({
                elem: '#uploadfile', //绑定元素
                url: localStorage.getItem('hosttingaddr') + '/api/moudlecfgfile/uploadfile',//上传接口
                cache: false,
                headers: {
                    user: getCookie("userinfo"),
                    version: getCookie("version")
                },
                data: {
                    dir: function () {
                       return getrequest().dir;
                    }
                },
                acceptMime: "application/json",
                accept: "file",
                exts: "json",
                multiple: true,//允许多文件上传

                done: function (res) {
                    //上传完毕回调
                    layer.msg(res.message);
                    //上传成功后进行数据刷新
                    table.reload("caseelement");
                },
                error: function () {
                    //请求异常回调
                    layer.msg("文件上传失败！");
                }
            });

        });
    </script>
</body>
</html>